<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.3.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.15.3/css/all.min.css">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css">

<script class="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"example.com","root":"/","images":"/images","scheme":"Muse","version":"8.3.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":false,"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"Searching...","empty":"We didn't find any results for the search: ${query}","hits_time":"${hits} results found in ${time} ms","hits":"${hits} results found"}};
  </script>
<meta name="description" content="Iterator简述iteratorIterator 的作用 为数据结构提供一个统一的、便捷的接口 使数据结构的成员能够按照次序排列 主要供for...of消费  Iterator 的遍历过程 创建一个指针对象，指向当前数据结构的起始位置 第一次调用指针对象的next方法，指向数据结构的第一个成员 第二次调用指针对象的next方法，指向数据结构的第二个成员 不断调用指针对象的next方法，知道它指">
<meta property="og:type" content="article">
<meta property="og:title" content="ES6 学习笔记 - iterator">
<meta property="og:url" content="http://example.com/2021/02/01/ES6/ES6-%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0-iterator/index.html">
<meta property="og:site_name" content="Lawted&#39;s Blog">
<meta property="og:description" content="Iterator简述iteratorIterator 的作用 为数据结构提供一个统一的、便捷的接口 使数据结构的成员能够按照次序排列 主要供for...of消费  Iterator 的遍历过程 创建一个指针对象，指向当前数据结构的起始位置 第一次调用指针对象的next方法，指向数据结构的第一个成员 第二次调用指针对象的next方法，指向数据结构的第二个成员 不断调用指针对象的next方法，知道它指">
<meta property="og:locale" content="en_US">
<meta property="article:published_time" content="2021-02-01T09:22:09.000Z">
<meta property="article:modified_time" content="2021-05-07T01:27:54.010Z">
<meta property="article:author" content="Lawted Wu">
<meta property="article:tag" content="ES6">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="http://example.com/2021/02/01/ES6/ES6-%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0-iterator/">


<script class="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'en'
  };
</script>
<title>ES6 学习笔记 - iterator | Lawted's Blog</title>
  




  <noscript>
  <style>
  body { margin-top: 2rem; }

  .use-motion .menu-item,
  .use-motion .sidebar,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header {
    visibility: visible;
  }

  .use-motion .header,
  .use-motion .site-brand-container .toggle,
  .use-motion .footer { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle,
  .use-motion .custom-logo-image {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line {
    transform: scaleX(1);
  }

  .search-pop-overlay, .sidebar-nav { display: none; }
  .sidebar-panel { display: block; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="Toggle navigation bar" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <h1 class="site-title">Lawted's Blog</h1>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu">
        <li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>Home</a></li>
        <li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>Tags</a></li>
        <li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>Archives</a></li>
  </ul>
</nav>




</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          Table of Contents
        </li>
        <li class="sidebar-nav-overview">
          Overview
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#Iterator"><span class="nav-number">1.</span> <span class="nav-text">Iterator</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E7%AE%80%E8%BF%B0iterator"><span class="nav-number">1.1.</span> <span class="nav-text">简述iterator</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#Iterator-%E7%9A%84%E4%BD%9C%E7%94%A8"><span class="nav-number">1.1.1.</span> <span class="nav-text">Iterator 的作用</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Iterator-%E7%9A%84%E9%81%8D%E5%8E%86%E8%BF%87%E7%A8%8B"><span class="nav-number">1.1.2.</span> <span class="nav-text">Iterator 的遍历过程</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E9%BB%98%E8%AE%A4%E7%9A%84iterator%E6%8E%A5%E5%8F%A3"><span class="nav-number">1.2.</span> <span class="nav-text">数据结构默认的iterator接口</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E4%B8%BA%E4%B8%80%E4%B8%AA%E5%AF%B9%E8%B1%A1%E6%B7%BB%E5%8A%A0Iterator-%E6%8E%A5%E5%8F%A3%E7%9A%84%E4%BE%8B%E5%AD%90"><span class="nav-number">1.2.1.</span> <span class="nav-text">为一个对象添加Iterator 接口的例子</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%B1%BB%E4%BC%BC%E6%95%B0%E7%BB%84%E7%9A%84%E5%AF%B9%E8%B1%A1%E8%B0%83%E7%94%A8%E6%95%B0%E7%BB%84%E7%9A%84Symbol-iterator%E6%96%B9%E6%B3%95%E7%9A%84%E4%BE%8B%E5%AD%90"><span class="nav-number">1.2.2.</span> <span class="nav-text">类似数组的对象调用数组的Symbol.iterator方法的例子</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%BB%80%E4%B9%88%E6%97%B6%E5%80%99%E8%B0%83%E7%94%A8iterator%E6%8E%A5%E5%8F%A3"><span class="nav-number">1.3.</span> <span class="nav-text">什么时候调用iterator接口</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%A7%A3%E6%9E%84%E8%B5%8B%E5%80%BC"><span class="nav-number">1.3.1.</span> <span class="nav-text">解构赋值</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%8B%93%E5%B1%95%E8%BF%90%E7%AE%97%E7%AC%A6"><span class="nav-number">1.3.2.</span> <span class="nav-text">拓展运算符</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#yield"><span class="nav-number">1.3.3.</span> <span class="nav-text">yield*</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%85%B6%E4%BB%96%E5%9C%BA%E5%90%88"><span class="nav-number">1.3.4.</span> <span class="nav-text">其他场合</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%AD%97%E7%AC%A6%E4%B8%B2%E7%9A%84iterator%E6%8E%A5%E5%8F%A3"><span class="nav-number">1.4.</span> <span class="nav-text">字符串的iterator接口</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E9%81%8D%E5%8E%86%E5%99%A8%E5%AF%B9%E8%B1%A1%E4%B8%AD%E7%9A%84return-throw"><span class="nav-number">1.5.</span> <span class="nav-text">遍历器对象中的return(),throw()</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#for%E2%80%A6of-%E5%BE%AA%E7%8E%AF"><span class="nav-number">1.6.</span> <span class="nav-text">for…of 循环</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#Set%E5%92%8CMap%E7%BB%93%E6%9E%84"><span class="nav-number">1.6.1.</span> <span class="nav-text">Set和Map结构</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%89%B9%E6%AE%8A%E7%9A%84%E9%81%8D%E5%8E%86"><span class="nav-number">1.6.2.</span> <span class="nav-text">特殊的遍历</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E7%B1%BB%E6%95%B0%E7%BB%84"><span class="nav-number">1.6.2.1.</span> <span class="nav-text">类数组</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%AF%B9%E8%B1%A1"><span class="nav-number">1.6.2.2.</span> <span class="nav-text">对象</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#jQuery%E5%AF%B9%E8%B1%A1"><span class="nav-number">1.6.2.3.</span> <span class="nav-text">jQuery对象</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E4%B8%8E%E5%85%B6%E4%BB%96%E7%9A%84%E8%AF%AD%E6%B3%95%E5%AF%B9%E6%AF%94"><span class="nav-number">1.6.3.</span> <span class="nav-text">与其他的语法对比</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%8E%9F%E5%A7%8B%E5%86%99%E6%B3%95"><span class="nav-number">1.6.3.1.</span> <span class="nav-text">原始写法</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#forEach%E5%86%99%E6%B3%95"><span class="nav-number">1.6.3.1.1.</span> <span class="nav-text">forEach写法</span></a></li></ol></li><li class="nav-item nav-level-4"><a class="nav-link" href="#for%E2%80%A6in-%E5%86%99%E6%B3%95"><span class="nav-number">1.6.3.2.</span> <span class="nav-text">for…in 写法</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#for-of-%E5%86%99%E6%B3%95%E2%9C%94"><span class="nav-number">1.6.3.3.</span> <span class="nav-text">for..of 写法✔</span></a></li></ol></li></ol></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">Lawted Wu</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">67</span>
          <span class="site-state-item-name">posts</span>
        </a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">8</span>
        <span class="site-state-item-name">tags</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author site-overview-item animated">
      <span class="links-of-author-item">
        <a href="https://github.com/LAWTED" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;LAWTED" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
  </div>



        </div>
      </div>
        <div class="back-to-top animated" role="button" aria-label="Back to top">
          <i class="fa fa-arrow-up"></i>
          <span>0%</span>
        </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="reading-progress-bar"></div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="en">
    <link itemprop="mainEntityOfPage" href="http://example.com/2021/02/01/ES6/ES6-%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0-iterator/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="Lawted Wu">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Lawted's Blog">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          ES6 学习笔记 - iterator
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">Posted on</span>

      <time title="Created: 2021-02-01 17:22:09" itemprop="dateCreated datePublished" datetime="2021-02-01T17:22:09+08:00">2021-02-01</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">Edited on</span>
        <time title="Modified: 2021-05-07 09:27:54" itemprop="dateModified" datetime="2021-05-07T09:27:54+08:00">2021-05-07</time>
      </span>

  
    <span class="post-meta-item" title="Views" id="busuanzi_container_page_pv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="far fa-eye"></i>
      </span>
      <span class="post-meta-item-text">Views: </span>
      <span id="busuanzi_value_page_pv"></span>
    </span>
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <h1 id="Iterator"><a href="#Iterator" class="headerlink" title="Iterator"></a>Iterator</h1><h2 id="简述iterator"><a href="#简述iterator" class="headerlink" title="简述iterator"></a>简述iterator</h2><h3 id="Iterator-的作用"><a href="#Iterator-的作用" class="headerlink" title="Iterator 的作用"></a>Iterator 的作用</h3><ul>
<li>为数据结构提供一个统一的、便捷的接口</li>
<li>使数据结构的成员能够按照次序排列</li>
<li>主要供<code>for...of</code>消费</li>
</ul>
<h3 id="Iterator-的遍历过程"><a href="#Iterator-的遍历过程" class="headerlink" title="Iterator 的遍历过程"></a>Iterator 的遍历过程</h3><ol>
<li>创建一个指针对象，指向当前数据结构的起始位置</li>
<li>第一次调用指针对象的next方法，指向数据结构的第一个成员</li>
<li>第二次调用指针对象的next方法，指向数据结构的第二个成员</li>
<li>不断调用指针对象的next方法，知道它指向数据结构的结束位置</li>
</ol>
<h2 id="数据结构默认的iterator接口"><a href="#数据结构默认的iterator接口" class="headerlink" title="数据结构默认的iterator接口"></a>数据结构默认的iterator接口</h2><h3 id="为一个对象添加Iterator-接口的例子"><a href="#为一个对象添加Iterator-接口的例子" class="headerlink" title="为一个对象添加Iterator 接口的例子"></a>为一个对象添加Iterator 接口的例子</h3><figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">//为一个对象添加Iterator 接口的例子</span></span><br><span class="line"><span class="keyword">let</span> obj = &#123;</span><br><span class="line">  data:[<span class="string">&quot;hello&quot;</span>,<span class="string">&quot;world&quot;</span>],</span><br><span class="line">  [<span class="built_in">Symbol</span>.iterator]()&#123;</span><br><span class="line">    <span class="keyword">const</span> self= <span class="built_in">this</span>;</span><br><span class="line">    <span class="keyword">let</span> index =<span class="number">0</span>;</span><br><span class="line">    <span class="keyword">return</span> &#123;</span><br><span class="line">      next ()&#123;</span><br><span class="line">        <span class="keyword">if</span>(index&lt;self.data.length)&#123;</span><br><span class="line">          <span class="keyword">return</span> &#123;</span><br><span class="line">            value :self.data[index++],</span><br><span class="line">            dong : <span class="literal">false</span></span><br><span class="line">          &#125;;</span><br><span class="line">        &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">          <span class="keyword">return</span> &#123;<span class="attr">value</span> :<span class="literal">undefined</span>,<span class="attr">done</span>:<span class="literal">true</span>&#125;;</span><br><span class="line">        &#125;</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span>(<span class="keyword">let</span> item <span class="keyword">of</span> obj)&#123;</span><br><span class="line">  <span class="built_in">console</span>.log(item);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<h3 id="类似数组的对象调用数组的Symbol-iterator方法的例子"><a href="#类似数组的对象调用数组的Symbol-iterator方法的例子" class="headerlink" title="类似数组的对象调用数组的Symbol.iterator方法的例子"></a>类似数组的对象调用数组的Symbol.iterator方法的例子</h3><figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">let</span> iterable = &#123;</span><br><span class="line">  <span class="number">0</span>: <span class="string">&#x27;a&#x27;</span>,</span><br><span class="line">  <span class="number">1</span>: <span class="string">&#x27;b&#x27;</span>,</span><br><span class="line">  <span class="number">2</span>: <span class="string">&#x27;c&#x27;</span>,</span><br><span class="line">  length: <span class="number">3</span>,</span><br><span class="line">  [<span class="built_in">Symbol</span>.iterator]: <span class="built_in">Array</span>.prototype[<span class="built_in">Symbol</span>.iterator]</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span> (<span class="keyword">let</span> item <span class="keyword">of</span> iterable) &#123;</span><br><span class="line">  <span class="built_in">console</span>.log(item);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>而普通的对象如果部署数组的Symbol.iterator就没有用</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">let</span> iterable = &#123;</span><br><span class="line">  a: <span class="string">&#x27;a&#x27;</span>,</span><br><span class="line">  b: <span class="string">&#x27;b&#x27;</span>,</span><br><span class="line">  b: <span class="string">&#x27;c&#x27;</span>,</span><br><span class="line">  length: <span class="number">3</span>,</span><br><span class="line">  [<span class="built_in">Symbol</span>.iterator]: <span class="built_in">Array</span>.prototype[<span class="built_in">Symbol</span>.iterator]</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span> (<span class="keyword">let</span> item <span class="keyword">of</span> iterable) &#123;</span><br><span class="line">  <span class="built_in">console</span>.log(item);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><strong>Symbol.iterator一定要返回一个遍历器对象，否则会报错</strong></p>
<p>有了遍历器接口，数据结构就可以用for..of循环遍历，也可以使用while循环遍历</p>
<h2 id="什么时候调用iterator接口"><a href="#什么时候调用iterator接口" class="headerlink" title="什么时候调用iterator接口"></a>什么时候调用iterator接口</h2><h3 id="解构赋值"><a href="#解构赋值" class="headerlink" title="解构赋值"></a>解构赋值</h3><figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">let</span> set = <span class="keyword">new</span> <span class="built_in">Set</span>().add(<span class="string">&#x27;a&#x27;</span>).add(<span class="string">&#x27;b&#x27;</span>).add(<span class="string">&#x27;c&#x27;</span>);</span><br><span class="line"><span class="keyword">let</span>[x,y] = set;</span><br><span class="line"><span class="built_in">console</span>.log(x); <span class="comment">//a</span></span><br><span class="line"><span class="built_in">console</span>.log(y); <span class="comment">//b</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span>[first,...rest] = set;</span><br><span class="line"><span class="built_in">console</span>.log(first); <span class="comment">//a</span></span><br><span class="line"><span class="built_in">console</span>.log(rest);  <span class="comment">//[&#x27;b&#x27;,&#x27;c&#x27;]</span></span><br></pre></td></tr></table></figure>


<h3 id="拓展运算符"><a href="#拓展运算符" class="headerlink" title="拓展运算符"></a>拓展运算符</h3><figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> str = <span class="string">&#x27;hello&#x27;</span></span><br><span class="line"><span class="built_in">console</span>.log([...str]); <span class="comment">//[ &#x27;h&#x27;, &#x27;e&#x27;, &#x27;l&#x27;, &#x27;l&#x27;, &#x27;o&#x27; ]</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> arr = [<span class="string">&#x27;b&#x27;</span>,<span class="string">&#x27;c&#x27;</span>];</span><br><span class="line"><span class="built_in">console</span>.log([<span class="string">&#x27;a&#x27;</span>,...arr,<span class="string">&#x27;d&#x27;</span>]); <span class="comment">//[ &#x27;a&#x27;, &#x27;b&#x27;, &#x27;c&#x27;, &#x27;d&#x27; ]</span></span><br></pre></td></tr></table></figure>


<h3 id="yield"><a href="#yield" class="headerlink" title="yield*"></a>yield*</h3><figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">let</span> generator = <span class="function"><span class="keyword">function</span>* (<span class="params"></span>)</span>&#123;</span><br><span class="line">  <span class="keyword">yield</span> <span class="number">1</span>;</span><br><span class="line">  <span class="keyword">yield</span>* [<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>];</span><br><span class="line">  <span class="keyword">yield</span> <span class="number">5</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">var</span> iterator = generator();</span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(iterator.next());</span><br><span class="line"><span class="built_in">console</span>.log(iterator.next());</span><br><span class="line"><span class="built_in">console</span>.log(iterator.next());</span><br><span class="line"><span class="built_in">console</span>.log(iterator.next());</span><br><span class="line"><span class="built_in">console</span>.log(iterator.next());</span><br><span class="line"><span class="comment">//&#123; value: 1, done: false &#125;</span></span><br><span class="line"><span class="comment">//&#123; value: 2, done: false &#125;</span></span><br><span class="line"><span class="comment">//&#123; value: 3, done: false &#125;</span></span><br><span class="line"><span class="comment">//&#123; value: 4, done: false &#125;</span></span><br><span class="line"><span class="comment">//&#123; value: 5, done: false &#125;</span></span><br></pre></td></tr></table></figure>
<p>　function*() 是一个函数生成器</p>
<p>　1. 函数生成器特点是函数名前面有一个‘*’<br> 　2. 通过调用函数生成一个控制器<br> 　3. 调用next()方法开始执行函数<br> 　4. 遇到yield函数将暂停<br> 　5. 再次调用next()继续执行函数</p>
<h3 id="其他场合"><a href="#其他场合" class="headerlink" title="其他场合"></a>其他场合</h3><ul>
<li>for…of</li>
<li>Array.from()</li>
<li>Map(),Set(),WeakMap(),WeakSet()</li>
<li>Promise.all()</li>
<li>Promise.race()</li>
</ul>
<h2 id="字符串的iterator接口"><a href="#字符串的iterator接口" class="headerlink" title="字符串的iterator接口"></a>字符串的iterator接口</h2><figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> someString = <span class="string">&#x27;hell0&#x27;</span>;</span><br><span class="line"><span class="keyword">var</span> iterator = someString[<span class="built_in">Symbol</span>.iterator]();</span><br><span class="line"><span class="built_in">console</span>.log(iterator.next());</span><br><span class="line"><span class="built_in">console</span>.log(iterator.next());</span><br><span class="line"><span class="built_in">console</span>.log(iterator.next());</span><br><span class="line"><span class="built_in">console</span>.log(iterator.next());</span><br><span class="line"><span class="built_in">console</span>.log(iterator.next());</span><br><span class="line"><span class="built_in">console</span>.log(iterator.next());</span><br><span class="line"></span><br><span class="line">&#123; <span class="attr">value</span>: <span class="string">&#x27;h&#x27;</span>, <span class="attr">done</span>: <span class="literal">false</span> &#125;</span><br><span class="line">&#123; <span class="attr">value</span>: <span class="string">&#x27;e&#x27;</span>, <span class="attr">done</span>: <span class="literal">false</span> &#125;</span><br><span class="line">&#123; <span class="attr">value</span>: <span class="string">&#x27;l&#x27;</span>, <span class="attr">done</span>: <span class="literal">false</span> &#125;</span><br><span class="line">&#123; <span class="attr">value</span>: <span class="string">&#x27;l&#x27;</span>, <span class="attr">done</span>: <span class="literal">false</span> &#125;</span><br><span class="line">&#123; <span class="attr">value</span>: <span class="string">&#x27;0&#x27;</span>, <span class="attr">done</span>: <span class="literal">false</span> &#125;</span><br><span class="line">&#123; <span class="attr">value</span>: <span class="literal">undefined</span>, <span class="attr">done</span>: <span class="literal">true</span> &#125;</span><br></pre></td></tr></table></figure>


<h2 id="遍历器对象中的return-throw"><a href="#遍历器对象中的return-throw" class="headerlink" title="遍历器对象中的return(),throw()"></a>遍历器对象中的return(),throw()</h2><p>return()是一个方法，如果for..of循环提前退出，比如出现了break或者continue就会调用return方法，我们可以用return()来清理释放资源</p>
<p>throw()方法一般配合Generator函数使用</p>
<h2 id="for…of-循环"><a href="#for…of-循环" class="headerlink" title="for…of 循环"></a>for…of 循环</h2><p>for…of 循环可以代替数组实例的forEach()方法</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">const</span> arr = [<span class="string">&#x27;red&#x27;</span>,<span class="string">&#x27;green&#x27;</span>,<span class="string">&#x27;blue&#x27;</span>];</span><br><span class="line">arr.forEach(<span class="function"><span class="keyword">function</span>(<span class="params">element,index</span>)</span>&#123;</span><br><span class="line">  <span class="built_in">console</span>.log(element);</span><br><span class="line">  <span class="built_in">console</span>.log(index);</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line">red</span><br><span class="line"><span class="number">0</span></span><br><span class="line">green</span><br><span class="line"><span class="number">1</span></span><br><span class="line">blue</span><br><span class="line"><span class="number">2</span></span><br></pre></td></tr></table></figure>


<p>JavaScript 原有的for…in 循环，只能够获取键名，没法获取键值，而ES6的for…of循环，允许遍历获取键值</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> arr =[<span class="string">&#x27;a&#x27;</span>,<span class="string">&#x27;b&#x27;</span>,<span class="string">&#x27;c&#x27;</span>,<span class="string">&#x27;d&#x27;</span>,<span class="string">&#x27;e&#x27;</span>];</span><br><span class="line"><span class="keyword">for</span> (<span class="keyword">let</span> a <span class="keyword">in</span> arr)&#123;</span><br><span class="line">  <span class="built_in">console</span>.log(a);<span class="comment">// 0 1 2 3 4</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">for</span>(<span class="keyword">let</span> b <span class="keyword">of</span> arr)&#123;</span><br><span class="line">  <span class="built_in">console</span>.log(b);<span class="comment">//a b c d e</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<h3 id="Set和Map结构"><a href="#Set和Map结构" class="headerlink" title="Set和Map结构"></a>Set和Map结构</h3><p>Set 和Map结构具有iterator接口，直接用for…of循环</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> cities = <span class="keyword">new</span> <span class="built_in">Set</span>([<span class="string">&#x27;Beijing&#x27;</span>,<span class="string">&#x27;Wuhan&#x27;</span>,<span class="string">&#x27;Chongqin&#x27;</span>])</span><br><span class="line"><span class="keyword">for</span> (<span class="keyword">var</span> city <span class="keyword">of</span> cities)&#123;</span><br><span class="line">  <span class="built_in">console</span>.log(city);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> cities = <span class="keyword">new</span> <span class="built_in">Map</span>();</span><br><span class="line">cities.set(<span class="string">&quot;Beijing&quot;</span>, <span class="number">0</span>);</span><br><span class="line">cities.set(<span class="string">&quot;Shanghai&quot;</span>, <span class="number">1</span>);</span><br><span class="line">cities.set(<span class="string">&quot;Wuhan&quot;</span>, <span class="number">2</span>);</span><br><span class="line"><span class="keyword">for</span> (<span class="keyword">var</span> [name, value] <span class="keyword">of</span> cities) &#123;</span><br><span class="line">  <span class="built_in">console</span>.log(name + <span class="string">&quot;:&quot;</span> + value);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<p>三个方法直接可以用的</p>
<ul>
<li>entries()返回一个遍历器对象，用来遍历[键名，键值]</li>
<li>keys()返回一个遍历器对象，用来遍历所有的键名</li>
<li>values()返回一个遍历器对象，用来遍历所有的键值</li>
</ul>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">let</span> arr = [<span class="string">&#x27;a&#x27;</span>,<span class="string">&#x27;b&#x27;</span>,<span class="string">&#x27;c&#x27;</span>];</span><br><span class="line"><span class="keyword">for</span>(<span class="keyword">let</span> pair <span class="keyword">of</span> arr.entries())&#123;</span><br><span class="line">  <span class="built_in">console</span>.log(pair);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="特殊的遍历"><a href="#特殊的遍历" class="headerlink" title="特殊的遍历"></a>特殊的遍历</h3><h4 id="类数组"><a href="#类数组" class="headerlink" title="类数组"></a>类数组</h4><p>并不是所有的类似数组的对象都具有iterator接口，一个简便的解决方法，就是使用Array.from方法将其转化为数组</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">let</span> arraylike= &#123;<span class="attr">length</span>:<span class="number">2</span>,<span class="number">0</span>:<span class="string">&#x27;a&#x27;</span>,<span class="number">1</span>:<span class="string">&#x27;b&#x27;</span>&#125;;</span><br><span class="line"><span class="keyword">for</span>(<span class="keyword">let</span> x <span class="keyword">of</span> <span class="built_in">Array</span>.from(arraylike))&#123;</span><br><span class="line">  <span class="built_in">console</span>.log(x);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<h4 id="对象"><a href="#对象" class="headerlink" title="对象"></a>对象</h4><p>使用<code>obeject.keys </code>方法将对象的键名生成一个数组，然后遍历这个数组</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">for</span>(<span class="keyword">var</span> key <span class="keyword">of</span> <span class="built_in">Object</span>.keys(someObeject))&#123;</span><br><span class="line">  <span class="built_in">console</span>.log(key+<span class="string">&#x27; : &#x27;</span>+someObject[key]);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<h4 id="jQuery对象"><a href="#jQuery对象" class="headerlink" title="jQuery对象"></a>jQuery对象</h4><p>加上一行：</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">jQuery.prototype[symbol.iterator] &#x3D; Array.prototype[Symbol.iterator];</span><br></pre></td></tr></table></figure>


<h3 id="与其他的语法对比"><a href="#与其他的语法对比" class="headerlink" title="与其他的语法对比"></a>与其他的语法对比</h3><h4 id="原始写法"><a href="#原始写法" class="headerlink" title="原始写法"></a>原始写法</h4><figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">for</span>(<span class="keyword">var</span> index=<span class="number">0</span>;index&lt;myArray.length;index++)&#123;</span><br><span class="line">  <span class="built_in">console</span>.log(myArray[index]);</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//缺点：麻烦</span></span><br></pre></td></tr></table></figure>
<h5 id="forEach写法"><a href="#forEach写法" class="headerlink" title="forEach写法"></a>forEach写法</h5><figure class="highlight js"><table><tr><td class="code"><pre><span class="line">myArray.forEach(<span class="function"><span class="keyword">function</span>(<span class="params">value</span>)</span>&#123;</span><br><span class="line">  <span class="built_in">console</span>.log(value);</span><br><span class="line">&#125;);</span><br><span class="line"><span class="comment">//缺点：无法中途跳出循环</span></span><br></pre></td></tr></table></figure>
<h4 id="for…in-写法"><a href="#for…in-写法" class="headerlink" title="for…in 写法"></a>for…in 写法</h4><figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">for</span> (<span class="keyword">var</span> index <span class="keyword">in</span> myArray)&#123;</span><br><span class="line">	<span class="built_in">console</span>.log(myArray[index]);</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//缺点：主要是为遍历对象设计的</span></span><br></pre></td></tr></table></figure>
<h4 id="for-of-写法✔"><a href="#for-of-写法✔" class="headerlink" title="for..of 写法✔"></a>for..of 写法✔</h4><figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">for</span>(<span class="keyword">let</span> value <span class="keyword">of</span> myArray)&#123;</span><br><span class="line">  <span class="built_in">console</span>.log(value);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>可以使用break</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">for</span>(<span class="keyword">var</span> n offibonacci)&#123;</span><br><span class="line">	<span class="keyword">if</span>(&gt;<span class="number">1000</span>)</span><br><span class="line">        <span class="keyword">break</span>;</span><br><span class="line">    <span class="built_in">console</span>.log(n);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
    </div>

    
    
    

    <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/ES6/" rel="tag"># ES6</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/2021/02/01/LEETCODE%20621%20%E4%BB%BB%E5%8A%A1%E8%B0%83%E5%BA%A6%E5%99%A8/" rel="prev" title="LEETCODE 621 任务调度器">
                  <i class="fa fa-chevron-left"></i> LEETCODE 621 任务调度器
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/2021/02/01/ES6/ES6-%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0-Destructing/" rel="next" title="ES6 学习笔记 - Destructing">
                  ES6 学习笔记 - Destructing <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>







<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      const activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      const commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>
</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Lawted Wu</span>
</div>
<div class="busuanzi-count">
    <span class="post-meta-item" id="busuanzi_container_site_uv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="Total Visitors">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-item" id="busuanzi_container_site_pv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="Total Views">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>

    </div>
  </footer>

  


  <script src="https://cdn.jsdelivr.net/npm/darkmode-js@1.5.7/lib/darkmode-js.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/animejs@3.2.1/lib/anime.min.js"></script>


<script>
var options = {
  bottom: '64px', // default: '32px'
  right: '32px', // default: '32px'
  left: 'unset', // default: 'unset'
  time: '0.5s', // default: '0.3s'
  mixColor: '#fff', // default: '#fff'
  backgroundColor: '#fff',  // default: '#fff'
  buttonColorDark: '#000',  // default: '#100f2c'
  buttonColorLight: '#fff', // default: '#fff'
  saveInCookies: true, // default: true,
  label: ' ', // default: ''
  autoMatchOsTheme: true // default: true
}
const darkmode = new Darkmode(options);
darkmode.showWidget();
</script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/schemes/muse.js"></script><script src="/js/next-boot.js"></script>

  






  
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>





</body>
</html>
